#include "buildAccelerator.h"
#include "buildPrimitive.h"

#include "accelerators/bvh.h"
#include "accelerators/grid.h"
#include "accelerators/kdtreeaccel.h"

#include <iostream>

Aggregate *getAccelerator(FnKat::Render::RenderSettings &renderSettings,
                          FnKat::FnScenegraphIterator rootIt)
{
    string accelType = "bvh";
    FnKat::GroupAttribute accelParamsAttr;
    FnKat::GroupAttribute globalSettingsAttr = rootIt.getAttribute("pbrtGlobalStatements");

    if(globalSettingsAttr.isValid())
    {
        FnKat::GroupAttribute accelAttr = globalSettingsAttr.getChildByName("accellerator");
        if(accelAttr.isValid())
        {
            FnKat::StringAttribute accellTypeAttr = accelAttr.getChildByName("type");
            if(accellTypeAttr.isValid())
                accelType = accellTypeAttr.getValue();

            accelParamsAttr = accelAttr.getChildByName(accelType);
        }
    }

    ParamSet p;
    vector<Reference<Primitive> > primitives;
    int numPrims = getPrimitives(renderSettings, rootIt, primitives);

    cout << "Found " << numPrims << " primitives..." << endl;

    if(accelType == "bvh")
    {
        setStringParam(p, accelParamsAttr, "splitMethod", "splitmethod");
        setIntParam(p, accelParamsAttr, "maxPrimitivesPerNode", "maxnodeprims");

        return CreateBVHAccelerator(primitives, p);
    }

    return NULL;
}

